<?php
$item                        = $this->item;
$display_photo               = isset($this->display_photo) ? $this->display_photo : true;;
$display_user                = isset($this->display_user) ? $this->display_user : true;
$display_customization       = isset($this->display_customization) ? $this->display_customization : true;
$display_price               = isset($this->display_price) ? $this->display_price : true;
$display_date                = isset($this->display_date) ? $this->display_date : true;
$display_category            = isset($this->display_category) ? $this->display_category : true;
$display_photo_if_not_exists = isset($this->display_photo_if_not_exists) ? $this->display_photo_if_not_exists : true;
$display_function            = isset($this->display_function) ? $this->display_function : true;
$display_modify            	 = isset($this->display_modify) ? $this->display_modify: false; //user's update buttons
$display_checkbox            = isset($this->display_checkbox) ? $this->display_checkbox : false; //my ads' checkbox
$display_view            	 = isset($this->display_view) ? $this->display_view : false; //ads view
$replace_like_with_follow    = isset($this->replace_like_with_follow) ? $this->replace_like_with_follow : false;
$lazy_loading                = isset($this->lazy_loading) ? $this->lazy_loading : true;

$photo_width                 = !empty($this->photo_width) ? $this->photo_width : 188;
$photo_height                = !empty($this->photo_height) ? $this->photo_height : 188;
$photo_params                = !empty($this->photo_params) ? $this->photo_params : array();
$photo_method                = !empty($this->photo_method) ? $this->photo_method : 'scale';

if(empty($item)) {
	return '';
}
if ($item instanceof Model_Company) {
    $type    = 'company';
    $company = $item;
    $user    = $company;
    $listing = null;
} elseif ($item instanceof Model_User) {
    $type = 'user';
    $company = $item;
    $user    = $item->company;
    $listing = $item;
} else {
    $type = 'listing';
    $company = new StdClass();
    if (!empty($item->user->cid)) {
        $company = $item->user->company;
    }
    $user    = $item->user;
    $listing = $item;
}

//user specific items
$is_like = Model_Like::isLiked($item->_id);
$like_button = '<a href="#" rel="'.$this->geturl(($is_like ? 'unlike' : 'like'),'follow', array('id' => (string)$item->_id)).'" class="like to_like '.($is_like ? 'selected' : '').'"><span>喜欢<em>'.Model_Like::getCount($item->_id).'</em></span></a>';

$bookmark = !empty($this->boomark) ? $this->bookmark : array();
$save_button = '<a href="#" class="save to_bookmark '.(in_array($item->_id, $bookmark) ? 'selected' : '') .'" rel="'.(string)$item->_id.'"><span>收藏</span></a>';

if($display_user) {
    $is_follow = Model_Follow::isFollowing($user);
    if(!empty($item->user->cid)) {
        $follow_button = '<a href="#" rel="'.$this->geturl(($is_follow ? 'unfollow' : 'follow'),'follow', array('id' => (string)$item->user->_id)).'" class="follow to_follow '.($is_follow ? 'selected' : '').'"><span>关注<em>'.Model_Follow::getCountFollowers($item->user->_id).'</em></span></a>';
    } else {
        $follow_button = '<a href="#" rel="'.$this->geturl(($is_follow ? 'unfollow' : 'follow'),'follow', array('id' => (string)$user->_id)).'" class="follow to_follow '.($is_follow ? 'selected' : '').'"><span>关注<em>'.Model_Follow::getCountFollowers($user->_id).'</em></span></a>';
    }
}

if($replace_like_with_follow) {
    $is_follow = Model_Follow::isFollowing($user);
    if(!empty($item->cid)) {
        $follow_button = '<a href="#" rel="'.$this->geturl(($is_follow ? 'unfollow' : 'follow'),'follow', array('id' => (string)$item->_id)).'" class="follow to_follow '.($is_follow ? 'selected' : '').'"><span>关注<em>'.Model_Follow::getCountFollowers($item->_id).'</em></span></a>';
    } else {
        $follow_button = '<a href="#" rel="'.$this->geturl(($is_follow ? 'unfollow' : 'follow'),'follow', array('id' => (string)$item->_id)).'" class="follow to_follow '.($is_follow ? 'selected' : '').'"><span>关注<em>'.Model_Follow::getCountFollowers($item->_id).'</em></span></a>';
    }
}

$key = array(
    (int)$display_photo, 
    (int)$display_user,  
    (int)$display_customization, 
    (int)$display_price,
    (int)$display_date,
    (int)$display_category, 
    (int)$display_photo_if_not_exists,
    (int)$display_function,
    (int)$replace_like_with_follow,
    $photo_width,
    $photo_height,
    count($photo_params),
    $photo_method,
    $lazy_loading,
);
$key = implode($key);
$key = md5($key);

$class_lazy = ' src=';
if ($lazy_loading) {
    $class_lazy = ' class="lazy" src="/images/grey.png" data-original=';
}

//$key = $type . '_'.(string)$item->_id . '_element_' . (int)$display_photo .'_'. (int)$display_user .'_'. (int)$display_customization.'_'.(int)$display_price.'_'.(int)$display_category.'_'.(int)$display_date.'_'.(int)$display_photo_if_not_exists.'_'.$photo_width.'_'.$photo_height.'_'.$photo_method.'_'.(int)$replace_like_with_follow;
$key = $type . '_'.(string)$item->_id . '_element_' . $key;

if (!defined('LISTING-ELEMENT-LAZY-LOAD')) {
    define('LISTING-ELEMENT-LAZY-LOAD', 1);
    $this->JQuery()->addJavascriptFile(Ac_Util::getImageBaseUrl('/js/jquery.lazyload.min.v-'.AC_VERSION.'.js'));    
    $this->JQuery()->javascriptCaptureStart()?>
        $j("img.lazy").show().lazyload({ threshold : 400 });    
    <?php $this->JQuery()->javascriptCaptureEnd();
    
}


if (($output = $this->cacheOutput()->start($key)) === false):
    ?>
    <dl>
        <?php if($display_photo && ($display_photo_if_not_exists || $this->thumbnail()->fileExists($item->featured_photo))):?>
            <dt>
            <figure>
                <a href="<?php echo $item->url(false, null, null);?>">
                    <?php
                    $params[0] = $item->featured_photo;
                    $params[1] = $photo_width;
                    $params[2] = $photo_height;
                    $params = array_merge($params, $photo_params);
                    //ob_clean(); echo '<pre>';var_dump($photo_method, $params);exit;
                    $img = call_user_func_array(array($this->thumbnail(), $photo_method), $params);
                    //ob_clean(); echo '<pre>';var_dump($this->thumbnail()->getSize($img));exit;
                    //ob_clean(); echo '<pre>';var_dump(parse_url($img, PHP_URL_PATH));exit;
                    $size = $this->thumbnail()->getSize($img);
                    ?>
                    <img <?php echo $class_lazy;?>"<?php echo $img;?>" alt="<?php echo $item->title?>" width="<?php echo $size['width']?>" height="<?php echo $size['height']?>">
                </a>
            </figure>
            </dt>
        <?php endif;?>
        <dd class="title"><h2><a href="<?php echo $item->url(false, null, null, array('sid' => $this->search_id));?>">
            <?php 
                if ($type == 'user') {
                    echo $item->name;
                } elseif ($type == 'company') {
                    echo $item->company_name;
                } else {
                    echo $item->title;
                }
                if($item->sold) {
                    echo '<span class="sold"><em>已售</em></span>';
                }
            ?>
            </a></h2></dd>
        <?php
        if($display_price) {
            echo $this->price($item);
        }?>
        <?php if($display_function):?>
            <dd class="func">
                <?php if($replace_like_with_follow):?>
                    [FOLLOW_BUTTON]
                <?php else:?>
                    [LIKE_BUTTON]
                    [SAVE_BUTTON]
                <?php endif;?>
            </dd>
        <?php endif;?>
        <?php if ($display_category && !empty($item->category)):?>
            <dd class="category">
                <?php foreach($item->category as $category):?>
                    <?php $tree = $category->getParentTree(); $to_add = '';?>
                    <?php foreach($tree as $cat):?>
                        <?php echo $to_add;?><a href="<?php echo $cat->url()?>"><?php echo $cat->name?></a>
                        <?php $to_add = '»';?>
                    <?php endforeach;?>
                    <?php break;?>
                <?php endforeach;?>
            </dd>
        <?php endif;?>
        <?php   
            if($display_customization) {
                $classname = $item::getSearchResultPartialName();
                $this->partial()->setObjectKey('model');
                echo $this->partial('index/customization/'. $classname.'/gallery-detail.phtml', 'search', array(
                    'listing'       => $item,
                ));
            }
        ?>
        <?php if($display_date):?>
            <dd class="date"><?php echo date('Y年m月d日', $item->posted_at)?></dd>
        <?php endif;?>
        <?php if($display_view):?>
        	<dd class="func">
        		<span class="like">
        			<?php echo $this->t('喜欢<em>__1__</em>', null, $item->count_likes); ?>
				</span>
        	</dd>
        	<dd class="post_date"><?php echo date('Y年m月d日', $item->posted_at)?></dd>
        	<dd class="location"><?php echo $item->area_l1 . ', ' . $item->area_l2?></dd>
            <dd class="view"><?php echo (int) $item->number_views?></dd>
        <?php endif;?>
        <?php if($display_checkbox):?>
            <dd class="checkbox select"><input type="checkbox" value="<?php echo (string)$item->_id?>" name="chk[]" class="chk_select"></dd>
        <?php endif;?>
        <?php if($display_modify):?>
            <dd class="modify">
            	<a class ="edit" href="/listing/edit?id=<?php echo (string)$item->_id;?>&mode=1"><?php echo $this->t('修改')?></a>
                
				<?php if ($this->tab == Model_Listing::STATUS_PUBLISHED) {?>
                <a class ="relaunch" href="/listing/renew?id=<?php echo (string)$item->_id;?>&ac=renew&nb=1&tab=<?php if (isset($_GET['tab'])) {echo $_GET['tab'];} else { echo '1';}?>"><?php echo $this->t('刷新')?></a>
                <?php } ?>
                
                <?php if ($item->sold == Model_Listing::STATUS_MARK_SOLD) {?>
                <a class="sold" href="<?php echo $this->geturl('mark-as-un-sold','listing', null, null, array('id' => (string)$item->_id))?>"><?php echo $this->t('标记为未售出')?></a>
                <?php  } else {?>
                <a class="sold" href="<?php echo $this->geturl('mark-as-sold','listing', null, null, array('id' => (string)$item->_id))?>"><?php echo $this->t('标记为已售')?></a>
                <?php } ?>

                <?php if ($item->status == Model_Listing::STATUS_DRAFT):?>
                <a class="cancel" href="/listing/publish?id=<?php echo (string)$item->_id;?>&ac=publish&nb=1&tab=<?php if (isset($_GET['tab'])) {echo $_GET['tab'];} else { echo '1';}?>"><?php echo $this->t('发布')?></a>
                <?php else: ?>
                <a class="cancel" href="/listing/unpublish?id=<?php echo (string)$item->_id;?>&ac=unpublish&nb=1&tab=<?php if (isset($_GET['tab'])) {echo $_GET['tab'];} else { echo '1';}?>"><?php echo $this->t('撤销发布')?></a>
                <?php endif;?>
                
                <a class ="del" href="/listing/delete?id=<?php echo (string)$item->_id;?>&ac=delete&nb=1&tab=<?php if (isset($_GET['tab'])) {echo $_GET['tab'];} else { echo '1';}?>"><?php echo $this->t('删除')?></a>
				
				<!--a href="/listing/edit?id=<?php echo (string)$item->_id;?>&mode=1" class="edit">修改</a>
				<a href="/listing/renew?id=<?php echo (string)$item->_id;?>&ac=renew&nb=1&tab=<?php if (isset($_GET['tab'])) {echo $_GET['tab'];} else { echo '1';}?>" class="relaunch">重新发布</a>
				<a href="<?php echo $this->geturl('mark-as-un-sold','listing', null, null, array('id' => (string)$item->_id))?>" class="sold">标记为已售</a>
				<a href="#" class="cancel">撤销发布</a>
				<a href="/listing/delete?id=<?php echo (string)$item->_id;?>&ac=delete&nb=1&tab=<?php if (isset($_GET['tab'])) {echo $_GET['tab'];} else { echo '1';}?>" class="del">删除</a-->
			</dd>
        <?php endif;?>
        <?php if($display_user):?>
            <?php if(!empty($item->user->cid) || $item instanceof Model_Company):?>
                <?php $company = $item instanceof Model_company ? $item : $item->user->company?>
                <dd class="user">
                    <figure>
                       <a href="<?php echo $company->url();?>">
                           <img <?php echo $class_lazy;?>"<?php echo $this->thumbnail()->scale($company->image, 40, 40, true, true);?>" alt="<?php echo $company->name?>" width="40", height="40">
                       </a>
                   </figure>
                   <a href="<?php echo $company->url()?>" class="name"><?php echo $company->company_name?></a>
                    <?php if($display_function):?>
                        [FOLLOW_BUTTON]
                    <?php endif;?>
                </dd>
            <?php else:?>
                <dd class="user">
                    <figure>
                        <a href="<?php echo $user->url();?>">
                            <img <?php echo $class_lazy;?>"<?php echo $this->thumbnail()->scale($user->pp, 40, 40, true, true);?>" alt="<?php echo $item->title?>" width="40", height="40">
                        </a>
                    </figure>
                    <a href="<?php echo $user->url()?>" class="name"><?php echo $user->name?></a>
                    <?php if($display_function):?>
                        [FOLLOW_BUTTON]
                    <?php endif;?>
                    <?php echo $item->user->_id ?>
                    <span><?php echo $item->tagline?></span>
                </dd>
            <?php endif?>
        <?php endif?>
    </dl>
    <?php
        $output = $this->cacheOutput()->end(array('index', $key), 86400, null, false);

     
        $output = str_replace('[LIKE_BUTTON]', $like_button, $output);
        $output = str_replace('[SAVE_BUTTON]', $save_button, $output);
        if(!empty($follow_button)) {
            $output = str_replace('[FOLLOW_BUTTON]', $follow_button, $output);
        }
       
        echo $output;
        echo '<!-- notcached: '.$key.' -->';
    ?>
<?php else:?>
    <?php
        $output = str_replace('[LIKE_BUTTON]', $like_button, $output);
        $output = str_replace('[SAVE_BUTTON]', $save_button, $output);
        if(!empty($follow_button)) {
            $output = str_replace('[FOLLOW_BUTTON]', $follow_button, $output);
        }
    ?>
    <?php echo $output.'<!-- cached: '.$key.' -->';?>
<?php endif;?>
